package com.common.www.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public final class ClassUtils {

    /**
     * 对象赋值
     *
     * @param dest
     * @param orig
     * @return
     */
    public static void copyProperties(Object dest, Object orig) {
        BeanUtils.copyProperties(orig, dest);
    }

    /**
     * 对象转移赋值
     *
     * @param dest
     * @param orig
     * @return
     */
    public static <T> T copyProperties(Class<T> dest, Object orig) {
        try {
            T t = dest.newInstance();
            BeanUtils.copyProperties(orig, t);
            return t;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将集合转为指定类型的集合
     *
     * @param origList
     * @param destClass
     * @param <T>
     * @return
     */
    public static <T> List<T> copyProperties(Class<T> destClass, List origList) {
        return JSONObject.parseArray(JSON.toJSONString(origList), destClass);
    }


    public static <T> List<T> copyObjects(Class<T> dest, List<Object[]> objList) {
        try {
            List<T> ts=new ArrayList<>();
            for (Object[] object : objList) {
                T t = dest.newInstance();
                for (int i = 0; i < object.length; i++) {
                    BeanUtils.copyProperties(object[i],t);
                }
                ts.add(t);
            }
            return ts;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }



}
